package cn.NewsDaoImpl;

import cn.NewsDao.BaseDao;
import cn.NewsDao.NewsDao1;
import cn.entity.Detail;
import cn.entity.News1;
import cn.entity.News31;
import com.text5.News;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

public class NewsDaoImpl1 extends BaseDao implements NewsDao1 {



    @Override
    public List<News1> getNewsList() {
        List<News1> newList=new ArrayList<News1>();
        try {
            //（3）获得Statement对象，执行SQL语句
            String sql="select id,categoryId,title,summary,content,author,createDate from news_detail ";
            Object[] params={};
            ResultSet rs=this.executeSQL(sql, params);
            //（4）处理执行结果(ResultSet)，
            while(rs.next()){
                int id=rs.getInt("id");
                int  categoryId=rs.getInt("categoryId");
                String title=rs.getString("title");
                String summary=rs.getString("summary");
                String content=rs.getString("content");
                String author=rs.getString("author");
                Timestamp time=rs.getTimestamp("createdate");


                //封装成新闻信息对象
                News1 news=new News1();
                news.setId(id);
                news.setCategoryId(categoryId);
                news.setTitle(title);
                news.setSummary(summary);
                news.setContent(content);
                news.setAuthor(author);
                news.setCreateDate(time);


                //将新闻对象放进集合中
                newList.add(news);
            }
        }catch (SQLException e) {

            e.printStackTrace();
        }finally{
            //释放资源
            this.closeResource();
        }
        return newList;
    }


    @Override
    public boolean add(News1 news) {
        boolean flag=false;
        try {
            String sql="insert into news_detail(categoryId,title) values(?,?)";
            Object[] params={news.getCategoryId(),news.getTitle()};
            int i=this.executeUpdate(sql, params);
            //（4）处理执行结果
            if(i>0){
                System.out.println("插入新闻成功！");
            }
            flag=true;
        }finally{
            //释放资源
            this.closeResource();
        }
        return flag;
    }

    @Override
    public boolean delete(int id) {
        boolean flag=false;
        try {
            String sql = "delete from  news_Detail where id=?";
            Object[] params={id};
            int i=this.executeUpdate(sql, params);
            //（4）处理执行结果
            if(i>0){
                System.out.println("删除新闻成功！");
            }
            flag=true;
        }  finally {
            //释放资源
            this.closeResource();
        }
        return flag;
    }

    @Override
    public boolean update(News1 news) {
        boolean flag=false;
        try {
            String sql = "update News_detail set title=? where id=?";
            Object[] params={news.getTitle(),news.getId()};
            int i=this.executeUpdate(sql, params);
            //（4）处理执行结果
            if(i>0){
                System.out.println("修改新闻成功！");
            }
            flag=true;
        }  finally {
            //释放资源
            this.closeResource();
        }
        return flag;
    }

    @Override
    public News1 selectById(int id) {
        List<News1> newList=new ArrayList<News1>();
        ResultSet resultSet =null;
        News1 news_comment = new News1();
        try {
            String  sql = "SELECT id,categoryId,title,picPath FROM news_detail WHERE id=?";
            Object[] obj={id};
            resultSet = this.executeSQL(sql, obj);
            while(resultSet.next()) {
                int id1 = resultSet.getInt("id");
                int newsId = resultSet.getInt("categoryId");
                String content = resultSet.getString("title");
                String author = resultSet.getString("picPath");

                news_comment.setId(id1);
                news_comment.setCategoryId(newsId);
                news_comment.setTitle(content);


            }
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            this.closeResource();
        }
        return news_comment;
    }



    @Override
    public List<News1> selectPage(int index, int crencount) {
        List<News1> newList=new ArrayList<News1>();
        int index1=index;
        int crencount1=1;
        String sql="SELECT * from news_detail LIMIT ?,?";
        Object[] objects = {index1,crencount1};
        ResultSet rs = this.selectPage(sql, objects);
        try {

            while(rs.next()){
                int id=rs.getInt("id");
                int  categoryId=rs.getInt("categoryId");
                String title=rs.getString("title");
                String picpath=rs.getString("picpath");


                //封装成新闻信息对象
                News1 news=new News1();
                news.setId(id);
                news.getCategoryId();

                news.setTitle(title);

                newList.add(news);


            }
        }catch (SQLException e) {

            e.printStackTrace();
        }finally{
            //释放资源
            this.closeResource();
        }


        return newList;
    }

    @Override
    public List<News1> selectPage1(Detail detail) {
        List<News1> newList2=new ArrayList<News1>();
        String sql="SELECT * from news_detail LIMIT ?,?";
        Object[] objects = {detail.getIndex(),detail.getCount()};
        ResultSet rs = this.selectPage(sql, objects);
        try {

            while(rs.next()){
                int id=rs.getInt("id");
                int  categoryId=rs.getInt("categoryId");
                String title=rs.getString("title");
                String picpath=rs.getString("picpath");


                //封装成新闻信息对象
                News1 news=new News1();
                news.setId(id);
                news.setCategoryId(categoryId);

                news.setTitle(title);

                newList2.add(news);
            }
        }catch (SQLException e) {

            e.printStackTrace();
        }finally{
            //释放资源
            this.closeResource();
        }


        return newList2;
    }


}
